<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ****************************************************************************
  NAIYUMIE KING WANG JJANG Front End Developer <naiyumie@gmail.com>
  FileType:CRLF, Encording:UTF-8, Tab&Space:4/4
  ------------------------------------------------------------------------
  Pagination 리스트 페이지네이션 함수

  출력 결과물
	<ul class='pagination nPagination'>
		<li class='disabled'><a href='#'>«</a></li>
		<li><a href='#'>1</a></li>
		<li><a href='#'>2</a></li>
		<li><a href='#'>3</a></li>
		<li><a href='#'>4</a></li>
		<li><a href='#'>5</a></li>
		<li><a href='#'>»</a></li>
	</ul>
**************************************************************************** */

class Npagination {

	public $paginationCfg;
	public function __construct() {
		$this->CI =& get_instance();
	}

	public function init($argArray = array()){
		$this->paginationCfg = array(
			'uri'				=> base_url().$argArray['seg1Arg'] .'/'. $argArray['seg2Arg'] .'/',
			'articles_count'	=> $argArray['articles_count'],
			'req_page'			=> empty($this->CI->input->get('p1')) ? 0 : $this->CI->input->get('p1'),
			'page_size'			=> $argArray['page_size'],
			'articles_cnt'		=> $argArray['totalCount'],
			'get'				=> $argArray['get']
		);
		$this->paginationCfg['paginationFirst'] = $this->paginationCfg['req_page'] * $this->paginationCfg['articles_count'];
		$this->paginationCfg['paginationSecond'] = $this->paginationCfg['articles_count'];
	}

	public function out(){

		$uri				= $this->paginationCfg["uri"];            # 이동할 URL
		$articles_page		= $this->paginationCfg["articles_count"]; # 한페이지에 보여줄 레코드 수
		$page				= $this->paginationCfg["req_page"];       # 현재 페이지
		$total				= $this->paginationCfg["articles_cnt"];   # 한페이지에 보여줄 갯수
		$page_size			= $this->paginationCfg["page_size"];      # 전체 레코드 수
		$get				= $this->paginationCfg["get"];            # 기준 $_GET키 이름

		$prt_first_page = NULL;
		$pt_page = NULL;
		$prt_end_page = NULL;

		if(strpos($uri,"?") === false) {
			$uri .= '?';
		} else {
			$uri .= '&';
		}

		//echo $uri;
		//echo $get;
		if(strpos($uri,$get) === true){
			//echo $uri;
		}

		if($total > 0){
			$total_page = intval(($total-1)/$articles_page);
			if (!$page) {
				$now_page = 0;
			} else {
				$now_page = $page;
			}
			$start_rec = ($now_page) * $articles_page;
			if (($start_rec+$articles_page)>$total) {
				$end_rec = $total;
			} else {
				$end_rec = $start_rec + $articles_page;
			}
			$first_page = intval($now_page/$page_size)*$page_size;

			if (($first_page+($page_size-1))>$total_page) {
				$last_page = $total_page;
			} else {
				$last_page = $first_page+($page_size-1);
			}

			if($first_page == 0) {
				$pt_page = '';
			} else {
				$prt_first_page = sprintf('<li><a href="javascript:void(0)" class="nPaginationPt" data-nPaginationVal="%s=0">처음</a></li>',$get);
				$prev_page = $first_page - 1;
				$pt_page = sprintf('<li><a href="javascript:void(0)" class="nPaginationPt" data-nPaginationVal="%s=%s"><span class="pagenation_prev">◀ %s</span></a></li> ',$get,$prev_page, "이전");
			}

			for ($j=$first_page; $j<=$last_page; $j++) {
				$dis_page = $j + 1;
				if ($j == $now_page) {
					$pt_page .= sprintf('<li class="active"><span class="curd">%s</span></li>',$dis_page);
				} else {
					$pt_page .= sprintf('<li><a href="javascript:void(0)" class="nPaginationPt" data-nPaginationVal="%s=%s"><span class="pagenation_link">%s</span></a></li>',$get,$j,$dis_page);
				}
			}
			if ($last_page != $total_page) {
				$next_page = $first_page+$page_size;
				$pt_page .= sprintf('<li><a href="javascript:void(0)" class="nPaginationPt" data-nPaginationVal="%s=%s"><span class="pagenation_next">%s ▶</span></a></li>',$get,$next_page, "다음");
				$prt_end_page = sprintf('<li><a href="javascript:void(0)" class="nPaginationPt" data-nPaginationVal="%s=%s">마지막</a></li>',$total_page,$total_page+1);
			} else {
				$pt_page .= '';
			}

			$dis_now_page = $now_page + 1;
			$dis_total_page = $total_page + 1;

			$pt_page = sprintf('<ul class="pagination nPagination">%s</ul>', $prt_first_page.$pt_page.$prt_end_page);
			return $pt_page;
		}
	}

}
/* End of file */